//
//  SDSharedViewController.m
//  SuperDoctor
//
//  Created by 年浩杰 on 16/7/9.
//  Copyright © 2016年 SD. All rights reserved.
//

#import "SDSharedViewController.h"
#import "SDSharedModel.h"
#import "SDSharedViewCell.h"
#import "SDSharedNibViewController.h"
@interface SDSharedViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSArray *dataArray;

@property (nonatomic,strong) UITableView *tableView;

@end

static NSString *identifier = @"cell";
@implementation SDSharedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self setupUI];
    
}

- (void)setupUI {
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.bottom.mas_equalTo(self.view);
        
    }];
    
    [self.tableView registerClass:[SDSharedViewCell class] forCellReuseIdentifier:identifier];
    

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SDSharedModel *model = self.dataArray[indexPath.row];
    
    SDSharedViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        
        cell = [[SDSharedViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.model = model;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SDSharedNibViewController *nib = [[SDSharedNibViewController alloc] init];

    
    [self.navigationController pushViewController:nib animated:YES];
    
    
}
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        
        self.tableView.delegate = self;
        
        self.tableView.dataSource = self;
        
        self.tableView.rowHeight = 80;
        
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}

- (NSArray *)dataArray {
    
    if (!_dataArray) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"AppShared.plist" ofType:nil];
        
        NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *mutableArray = [NSMutableArray array];
        
        for (NSDictionary *dict in tempArray) {
            
            SDSharedModel *model = [[SDSharedModel alloc] initWithDict:dict];
            
            [mutableArray addObject:model];
        }
        _dataArray = mutableArray;
    }
    return _dataArray;
}
@end
